package com.liner.result;


import lombok.Data;

@Data
public class Result<T> {
    private Integer code;
    private String msg;
    private T data;


    public static <T> Result<T> build(T body, Integer code, String message) {
        Result<T> result = new Result<T>();
        if (body != null) {
            result.setData(body);
        }
        result.setCode(code);
        result.setMsg(message);
        return result;
    }

    //操作成功
    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    public static <T> Result<T> ok(T data) {
        return build(data, 0, "成功");
    }


    //操作失败
    public static <T> Result<T> fail() {
        return Result.fail(null);
    }

    public static <T> Result<T> fail(T data) {
        return build(data, -1, "失败");
    }



    public Result<T> message(String msg) {
        this.setMsg(msg);
        return this;
    }

    public Result<T> code(Integer code) {
        this.setCode(code);
        return this;
    }
}
